package com.shsappdev.shsfalconbeta;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SearchViewCompat;
import android.widget.SearchView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.shsappdev.shsfalconbeta.article.Article;
import com.shsappdev.shsfalconbeta.article.ArticleChangeListener;
import com.shsappdev.shsfalconbeta.article.ArticleManager;

public class SearchActivity extends SherlockFragmentActivity implements ArticleChangeListener {
	
	private volatile boolean load;
	public String query;
	public Handler handler;
	private MenuItem refresh;
	
	public void setLoad(final boolean loading) {
		handler.post(new Runnable() {
			public void run() {
				if(load != loading) {
					load = loading;
					if(load) {
						refresh.setActionView(R.layout.actionbar_compat);
					} else {
						refresh.setActionView(null);
					}
				}
			}
		});
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new Handler();
		setContentView(R.layout.activity_search);
		((TextView)findViewById(R.id.searchText)).setText(R.string.searching); 
		Intent intent = getIntent();
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			query = intent.getStringExtra(SearchManager.QUERY);
		}
		Fragment fragment = new SearchFragment();
        getSupportFragmentManager().beginTransaction()
        .replace(R.id.main, fragment)
        .commit();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		ArticleManager.addListener(this);
	}
	
	@Override
	public boolean onPrepareOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		menu.clear();
		com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.activity_main, menu);
		final SearchView searchView = (SearchView)SearchViewCompat.newSearchView(getSupportActionBar().getThemedContext());
		final MenuItem search = menu.findItem(R.id.menu_search);
		refresh = menu.findItem(R.id.menu_refresh);
		searchView.setQueryHint(query);
		SearchViewCompat.setOnQueryTextListener(searchView,
				new SearchViewCompat.OnQueryTextListenerCompat() {

			@Override
			public boolean onQueryTextSubmit(String scrip_searched) {
				query = scrip_searched;
				((SearchFragment)getSupportFragmentManager().findFragmentById(R.id.main))
					.pushForMore(false);
				setSearching(0);
				searchView.setQueryHint(query);
				return false;
			}

			@Override
			public boolean onQueryTextChange(String newText) {
				return false;
			}
		});
		search.setActionView(searchView);
		search.expandActionView();
		return super.onCreateOptionsMenu(menu);
	}
	
	public void setResultsText(final int results) {
		handler.post(new Runnable() {
			public void run() {
				((TextView)findViewById(R.id.searchText)).setText("" + results + ((results == 1) ? " Result Found": " Results Found")); 
			}
		});
	}
	
	public void setSearching(final int results) {
		handler.post(new Runnable() {
			public void run() {
				((TextView)findViewById(R.id.searchText)).setText(getResources().getString(R.string.searching)); 
			}
		});
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		case R.id.menu_refresh:
			Fragment main = getSupportFragmentManager().findFragmentById(R.id.main);
			((SearchFragment)main).pushForMore(false);
			break;
		}
		return true;
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		ArticleManager.removeListener(this);
	}

	public void viewArticle(Article article) {
		Intent intent = new Intent(this, ArticleViewerActivity.class);
		intent.putExtra("article", article);
		startActivity(intent);
	}
	
	@Override
	public void onArticleUpdate() {
		 Fragment main = getSupportFragmentManager().findFragmentById(R.id.main);
		 ((SearchFragment)main).notifyArticleChange();
	}
	
	@Override
	public void onLoadStart() {
		setLoad(true);
		
	}

	@Override
	public void onLoadDone() {
		setLoad(false);
	}
	
}